package com.bizframework.security.domain.dao.impl;

import java.util.List;

import com.bizframework.springhb.support.dao.BaseDaoHBImpl;

import org.hibernate.Query;
import org.springframework.stereotype.Repository;

import com.bizframework.security.domain.dao.UserDao;
import com.bizframework.security.domain.entity.User;

@Repository("userDao")
public class UserDaoImpl extends BaseDaoHBImpl<User, String> implements UserDao {

	public User getByUserName(String userName) throws Exception {
		List<User> users = this.findBy("userName", userName);
		if (users.size() > 0) {
			return users.get(0);
		}
		return null;
	}

	@SuppressWarnings("unchecked")
	public List<String> getUserPermissionCodes(String userName) {
		StringBuffer hql = new StringBuffer("select p.code from Group g inner join g.permissions p inner join g.users u where u.userName=:userName");
		Query query = getSession().createQuery(hql.toString());
		query.setParameter("userName", userName);
		return query.list();
	}
}
